package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.interfaces.CheckItemService;
import cn.itcast.pojo.CheckItem;
import cn.itcast.pojo.PageResult;
import cn.itcast.pojo.QueryPageBean;
import cn.itcast.pojo.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: peterChen
 * @date: 2019/8/13 12:03
 * @description: TODO
 * @version: 1.0
 */
@RestController
@RequestMapping("checkitem")
public class CheckItemController {
    @Reference
    private CheckItemService checkItemService;

    private Logger log = Logger.getLogger(CheckItemController.class);

    /**
     * 添加检查项
     * @param checkItem
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")  //新增权限校验
    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        try {
            checkItemService.add(checkItem);
            return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            //e.printStackTrace();
            log.error("add checkitem error.",e);
            return new Result(false,MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    /**
     * 分页查询检查项
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = checkItemService.pageQuery(
                    queryPageBean.getCurrentPage(),
                    queryPageBean.getPageSize(),
                    queryPageBean.getQueryString()
            );
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,pageResult);
        } catch (Exception e) {
            //e.printStackTrace();
            log.error("findPage checkitem error.",e);
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * 根据id获取检查项的信息
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @RequestMapping("/findCheckItemById")
    public Result findCheckItemById(Integer id){
        try {
            CheckItem checkItem = checkItemService.findCheckItemById(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
        } catch (Exception e) {
            //e.printStackTrace();
            log.error("Query CheckItem By Id error.",e);
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * 提交编辑后的检查项
     * @param checkItem
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){
        try {
            checkItemService.edit(checkItem);
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            //e.printStackTrace();
            log.error("Edit CheckItem error.",e);
            return new Result(true,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    /***
     * 删除对应id的检查项
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            checkItemService.delete(id);
        } catch (RuntimeException r){
            return new Result(false,r.getMessage());
        } catch (Exception e) {
            //e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * 获取所有的检查项
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<CheckItem> checkItems =  checkItemService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItems);
        } catch (Exception e) {
            //e.printStackTrace();
            log.error("Query All CheckItem error.",e);
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}
